//---------------------------------------------------------------------------

#pragma hdrstop

#include "filelib.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

bool CLogFile::OpenLog(char* filepath)
{
	HFile = CreateFile(filepath,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
	if(HFile == INVALID_HANDLE_VALUE)  return 0;
	SetFilePointer(HFile,0,0,FILE_END);
	endline[0]=0x0D;
	endline[1]=0x0A;
	return 1;
}


bool CLogFile::WriteLog(char* str,DWORD flags,ULONG strlen)
{
	DWORD n,n1;
	char* buff;
	if(strlen==-1)  strlen=std::strlen(str);
	if(! (flags&LOGFILE_DONTADDTIME))
	{
		int timelen=GetTimeFormat(LOCALE_USER_DEFAULT,TIME_FORCE24HOURFORMAT,NULL,NULL,NULL,0);
		int datelen=GetDateFormat(LOCALE_USER_DEFAULT,DATE_SHORTDATE,NULL,NULL,NULL,0);
		buff=new char[strlen+timelen+datelen+3];
		GetDateFormat(LOCALE_USER_DEFAULT,DATE_SHORTDATE,NULL,NULL,buff,200);
		GetTimeFormat(LOCALE_USER_DEFAULT,TIME_FORCE24HOURFORMAT,NULL,NULL,buff+datelen,200);
		buff[datelen-1]=' ';
		strcpy(buff+timelen+datelen-1," : ");
		strcpy(buff+timelen+datelen+2,str);
		str=buff;
		strlen+=timelen+datelen+2;
		if(!WriteFile(HFile,str,strlen,&n,0))
		{
			delete []buff;
			return 0;
		}
		delete []buff;
	}  else
	if(!WriteFile(HFile,str,strlen,&n,0)) return 0;
	if(!(flags&LOGFILE_NOENDOFLINE) && !WriteFile(HFile,endline,2,&n1,0)) return 0;
	if(n!=strlen) return 0;  else  return 1;
}


void CLogFile::CloseLog()
{
	CloseHandle(HFile);
}

bool CLogFile::ResetLog()
{
	SetFilePointer(HFile,0,0,FILE_BEGIN);
	SetEndOfFile(HFile);
	return 1;
}
